home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: argc/argv & switches
- Date: 28 Feb 1996 16:28:25 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h2rv9INNbc2@anvil.ugrad.cs.ubc.ca>
- References: <4h2j8j$9gn@milo.freenet.vancouver.bc.ca>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4h2j8j$9gn@milo.freenet.vancouver.bc.ca>,
- Patrick Wong <zipz@opus.freenet.vancouver.bc.ca> wrote:
- >I would like to know the difference between *++argv[0] and (*++argv)[0].
- >Also, I've seen a lot of programs that have command-line switches like:
- >
- >view -r -x list.txt
- >view -rx list.txt
- >view -xr list.txt
- >
- >How can I write the view program so that it can accept all three formats?
- >It has something to do with the *argv[0] parameter, I know that much. I
- >looked at K&R and found the example program confusing (find -n -l pattern).
-
- Some systems have a function called getopt() for parsing command line options
- above. You would give it this string:
-
- "rxS:t"
-
- for instance, to indicate that you have four options, 'r', 'x', 'S' and 't',
- and the 'S' option takes an argument, like -S <arg>
-
- The GNU getopt also has a getopt_long() for long switches like "--option foo".
-
- These are not standard C functions by any means, but you can hunt down the
- freeware source that you can include in your program.
-
- >The view program would look like this:
- >/* begin program */
- >#include <stdio.h>
- >main()
- >{
- > int c;
- > while ((c = getchar()) != EOF)
- > putchar(c);
- >} /* ok, so it's a simple program... */
- >/* end program */
- >
- >How can this program be modified to use -r and -x?
- >-x : pause after a screenful of text. If less than screnful, ignore
- >-r : put line numbers in front of each line of text.
- >
- >This program is just something that will help me understand how the
- >*argv[0] thing works. Thanks.
-
- In that case, do it yourself! Or look at the code for the getopt() functions in
- the gnu "glibc" library (which you can get from a mirror of
- ftp://prep.ai.mit.edu/pub/gnu.
-
- There is also some basic argument parsing code in the K&R2 book. The FAQ might
- mention something as well.
-
- --
-
-